package wcg;

import java.awt.GridBagLayout;

import javax.swing.JFileChooser;
import javax.swing.JPanel;
import java.awt.Dimension;
import java.awt.BorderLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import javax.swing.JTextField;
import java.awt.Rectangle;
import java.io.File;
import java.util.Iterator;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.JCheckBox;

public class PanelWinExporter extends JPanel {

	private static final long serialVersionUID = 1L;
	private JPanel jPanelNorth = null;
	private JPanel jPanelCenter = null;
	/**
	 * This is the default constructor
	 */
	public PanelWinExporter() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	protected void initialize() {
		this.setSize(747, 220);
		this.setLayout(new BorderLayout());
		this.add(getJPanelNorth(), BorderLayout.NORTH);  // Generated
		this.add(getJPanelCenter(), BorderLayout.CENTER);  // Generated
	}

	/**
	 * This method initializes jPanelNorth	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelNorth() {
		if (jPanelNorth == null) {
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 0;  // Generated
			gridBagConstraints.gridy = 0;  // Generated
			jPanelNorth = new JPanel();
			jPanelNorth.setSize(new Dimension(400,300));
			jPanelNorth.setLayout(new BorderLayout());  // Generated
			jPanelNorth.add(getJScrollPane(), BorderLayout.NORTH);  // Generated
		}
		return jPanelNorth;
	}

	/**
	 * This method initializes jPanelCenter	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	protected JPanel getJPanelCenter() {
		if (jPanelCenter == null) {
			jLabel = new JLabel();
			jLabel.setBounds(new Rectangle(12, 5, 117, 24));  // Generated
			jLabel.setText("Convert WIN files ");  // Generated
			jPanelCenter = new JPanel();
			jPanelCenter.setLayout(null);  // Generated
			jPanelCenter.add(jLabel, null);  // Generated
			jPanelCenter.add(getJButtonDoConvert(), null);  // Generated
			jPanelCenter.add(getJComboBoxType(), null);  // Generated
			jPanelCenter.add(getJCheckBoxDeleteInputFile(), null);
		}
		return jPanelCenter;
	}
	public String getOutDir() {
		return OutDir;
	}

	public void setOutDir(String outDir) {
		OutDir = outDir;
	}
	/**
	 * 
	 */
	private String OutDir = new String("");  //  @jve:decl-index=0:
	private JLabel jLabel = null;
	private JButton jButtonDoConvert = null;
	private JComboBox jComboBoxType = null;
	/**
	 * 
	 * @return
	 */
	protected boolean outDirectorySelect(){
		if(OutDir.isEmpty()){
			Vector<String> fn = jTableFiles.getFileNames();
			String filepath = fn.get(0);
			if(filepath!=null){
				File f = new File(filepath);
				String path=f.getAbsolutePath();
				if(path!=null){
					OutDir=path;
				}
			}
		}
		String initPath = new String(OutDir);	
		JFileChooser filechooser = new JFileChooser(initPath); //@jve:decl-index=0
		filechooser.setFileSelectionMode( JFileChooser.DIRECTORIES_ONLY );	      
		filechooser.setDialogTitle("Save folder");
		int selected = filechooser.showSaveDialog(this);
		if ( selected == JFileChooser.APPROVE_OPTION ) {
			File file = filechooser.getSelectedFile ();
			String dirPath = file.getAbsolutePath();
			OutDir = new String(dirPath);
			return true;
		}
		return false;
	}

	private WinExporter WinConverter=null;
	private JScrollPane jScrollPane = null;
	private FileDropTable jTableFiles = null;
	private JCheckBox jCheckBoxDeleteInputFile = null;
	/**
	 * This method initializes jButtonDoConvert	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonDoConvert() {
		if (jButtonDoConvert == null) {
			jButtonDoConvert = new JButton();
			jButtonDoConvert.setBounds(new Rectangle(331, 4, 101, 27));  // Generated
			jButtonDoConvert.setText("Do Convert");  // Generated
			jButtonDoConvert.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if(outDirectorySelect()){
						WinConverter= new WinExporter();
						WinConverter.setOutDir(OutDir);
						switch(jComboBoxType.getSelectedIndex()){
						case 0:
							System.out.println("WIN file converter (win->ascii)");
							WinConverter.setOutType(0);
							break;
						case 1:
							System.out.println("WIN file converter (win->binary int32 big endian)");
							WinConverter.setOutType(1);
							WinConverter.setBigEndian(true);
							break;
						case 2:
							System.out.println("WIN file converter (win->binary int32 little endian)");
							WinConverter.setOutType(1);
							WinConverter.setBigEndian(false);
							break;
						}
						
						Vector<String> fn = jTableFiles.getFileNames();
						System.out.println("---------------------------");
						System.out.println(fn.size() + " input files");
						Iterator<String> ite = fn.iterator();
						while(ite.hasNext()){
							String s = ite.next();
							WinConverter.Convert(s);
							WinConverter.clear();
							if(jCheckBoxDeleteInputFile.isSelected()){
								File file = new File(s);
								file.delete();
								jTableFiles.RemoveItem(s);
								
							}
						}
//						DefaultTableModel mdl = (DefaultTableModel)jTableFiles.getModel();
//						int fmax = mdl.getRowCount();
//						System.out.println("---------------------------");
//						System.out.println(fmax + " input files");
//						for(int i=0;i<fmax;i++){
//							String f = (String)mdl.getValueAt(i,1);
//							if(f!=null){
//								if(!f.isEmpty()){
//									WinConverter.Convert(f);
//									WinConverter.clear();
//								}
//							}
//						}
						
//						
//						String files = jTextFieldFiles.getText();
//						String [] f = files.split(";");
//						System.out.println(f.length + " input files");
//						for(int i=0;i<f.length;i++){
//							WinConverter.Convert(f[i]);
//							WinConverter.clear();
//						}
						System.out.println("--------------------");
					}
				}
			});
		}
		return jButtonDoConvert;
	}

	/**
	 * This method initializes jComboBoxType	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getJComboBoxType() {
		if (jComboBoxType == null) {
			jComboBoxType = new JComboBox();
			jComboBoxType.setBounds(new Rectangle(137, 5, 183, 23));  // Generated
			jComboBoxType.addItem("Ascii");
			jComboBoxType.addItem("Binary (int32 big endian)");
			jComboBoxType.addItem("Binary (int32 little endian)");
			jComboBoxType.setSelectedIndex(0);
		}
		return jComboBoxType;
	}

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setPreferredSize(new Dimension(200,150));
			jScrollPane.setViewportView(getJTableFiles());  // Generated
		}
		return jScrollPane;
	}

	/**
	 * This method initializes jTableFiles	
	 * 	
	 * @return javax.swing.JTable	
	 */
	private JTable getJTableFiles() {
		if (jTableFiles == null) {
			jTableFiles = new FileDropTable();
//			jTableFiles = new JTable();
//			String[] col = { "No","File"};
//			String[][] dat = new String[1][2];
//			dat[0][0] =  "";
//			dat[0][1] = "Drop file here"; 
//			DefaultTableModel mdl = new DefaultTableModel(dat,col);	// @
//			jTableFiles.setModel(mdl);
//			TableColumn tc  = jTableFiles.getColumn("No");
//			tc.setMaxWidth(20);
//			jTableFiles.setDropTarget(new FileDropTarget());
//			jTableFiles.setEnabled(true);
		}
		return jTableFiles;
	}

	/**
	 * This method initializes jCheckBoxDeleteInputFile	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getJCheckBoxDeleteInputFile() {
		if (jCheckBoxDeleteInputFile == null) {
			jCheckBoxDeleteInputFile = new JCheckBox();
			jCheckBoxDeleteInputFile.setBounds(new Rectangle(26, 37, 236, 24));
			jCheckBoxDeleteInputFile.setText("Delete input file(s) after processing");
		}
		return jCheckBoxDeleteInputFile;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
